package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDao {
    // blog 表数据的操作方法 增删改查
    public void insert(Blog blog){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构建 sql 语句
            String sql = "insert into blog values(null, ?, ?, now(), ?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, blog.getTitle());
            statement.setString(2, blog.getContent());
            statement.setInt(3, blog.getUserId());
            // 3. 执行 sql 语句
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 4. 释放资源 - 最好通过 finally 释放资源，防止抛出异常后，没有执行释放资源的步骤
            DBUtil.close(connection, statement, null);
        }
    }
    public List<Blog> getBlogList(){
        List<Blog> blogList = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 sql 语句
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            // 3. 执行 sql 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果
            while(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                // 这里的 content 需要进行截断，博客列表页只显示摘要即可
                String content = resultSet.getString("content");
                if(content.length() > 100){
                    content = content.substring(0, 100) + "...";
                }
                blog.setContent(content);
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));
                blogList.add(blog);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 5. 释放资源
            DBUtil.close(connection, statement, resultSet);
        }

        return blogList;
    }
    public Blog getBlogByBlogId(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 sql 语句
            String sql = "select * from blog where blogId = " + blogId;
            statement = connection.prepareStatement(sql);
            // 3. 执行 sql 语句
            resultSet = statement.executeQuery();
            // 4. 遍历结果  - 得到的结果为 1 篇博客，因此使用 if 判断即可
            if(resultSet.next()){
                Blog blog = new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blog.setUserId(resultSet.getInt("userId"));

                return blog;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 5. 释放资源
            DBUtil.close(connection, statement, resultSet);
        }

        return null;
    }

    public void deleteBlogByBlogId(int blogId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 1. 建立连接
            connection = DBUtil.getConnection();
            // 2. 构造 sql 语句
            String sql = "delete from blog where blogId = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, blogId);
            // 3. 执行 sql 语句
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            // 4. 释放资源
            DBUtil.close(connection, statement, null);
        }
    }
}
